/**
 * CSS Loader
 */
(function(window, undefined) {
	var document = window.document;
	//
	var CSSManager = function() {
	};
	CSSManager.prototype = {
		timeout : null,
		timeval : null,
		csslist : null,
		//callback handler
		loadcallback : null,
		//timeout duration
		loadduration : 10000,
		loadstopped : false,
		loadstop : function() {
			this.loadstopped = true;
			clearTimeout(this.timeout);
			clearInterval(this.timeval);
		},
		loadcomplete : function() {
			this.loadstop();
			if (typeof (this.loadcallback) === "function") {
				this.loadcallback.call(this);
			}
			this.loadcallback = null;
		},
		//check styleSheets
		loadcheck : function() {
			if (this.loadstopped) {
				return this;
			}
			var num = 0;
			var sheets = document.styleSheets;
			var len = sheets.length;
			for ( var i = 0; i < len; i++) {
				var href = sheets[i].href;
				if (this.loadcheckhref(href)) {
					num++;
				}
			}
			var total = this.csslist.length;
			//
			//output(num + "/" + total);
			//all done
			if (num >= total) {
				this.loadcomplete();
				return;
			}
			return this;
		},
		loadcheckhref : function(href) {
			for ( var i = 0, l = this.csslist.length; i < l; i++) {
				var url = this.csslist[i];
				if (href == url) {
					return true;
				}
			}
			return false;
		},
		loadremove : function(link) {
			var href = link.href;
			for ( var i = 0; i < this.csslist.length; i++) {
				var url = this.csslist[i];
				if (href == url) {
					this.csslist.splice(i, 1);
					break;
				}
			}
			return this;
		},
		load : function(csslist, callback, duration) {
			this.loadcallback = callback;
			this.loadstop();
			if (csslist && !(csslist instanceof Array)) {
				csslist = [csslist];
			}
			this.csslist = [];
			var head = document.head;
			var self = this;
			for ( var i = 0, l = csslist.length; i < l; i++) {
				var href = csslist[i];
				if (!href) {
					continue;
				}
				var link = document.createElement('link');
				link.rel = "stylesheet";
				link.type = "text/css";
				link.href = "" + href;
				//IE/FF/Opera, Not Chrome
				link.onload = link.onerror = function() {
					self.loadremove(this);
				};
				head.appendChild(link);
				this.csslist.push(link.href);
			}
			//
			if (!this.csslist.length) {
				this.loadcomplete();
				return this;
			}
			//start
			this.loadstopped = false;
			//
			//final check
			this.timeval = setInterval(function() {
				self.loadcheck();
			}, 50);
			//timeout check
			duration = duration || this.loadduration;
			this.timeout = setTimeout(function() {
				//output("timeout");
				self.loadcomplete();
			}, duration);
			//
			return this;
		},
		remove : function(csslist, callback) {
			if (csslist && !(csslist instanceof Array)) {
				csslist = [csslist];
			}
			var len = csslist.length;
			if (len) {
				var temp = document.createElement("link");
				for ( var i = 0; i < len; i++) {
					var href = csslist[i];
					if (href) {
						temp.href = href;
						this.removehref(temp.href);
					}
				}
			}
			if (typeof (callback) === "function") {
				callback.call(this);
			}
			return this;
		},
		removehref : function(href) {
			var links = document.getElementsByTagName("link");
			for ( var i = 0, l = links.length; i < l; i++) {
				var link = links[i];
				if (link && link.href == href && link.parentNode) {
					link.parentNode.removeChild(link);
					break;
				}
			}
			return this;
		}
	};
	//
	function output(msg) {
		var body = document.body;
		var div = document.createElement('div');
		div.innerHTML = msg;
		body.appendChild(div);
	}
	window.onload = function() {
		output("document ready");
		//
		var csslist = [
		//
		"main.css",
		//
		"http://static.jquery.com/ui/css/base2.css",
		//
		"http://static.jquery.com/ui/themeroller/app_css/app_screen.css",
		//
		"http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css",
		//
		"http://www.google.com.hk/",
		//
		"http://icon.zol-img.com.cn/css/reset.css",
		//
		"http://icon.zol-img.com.cn/css/reset.css",
		//
		"",
		//
		"http://dcbbs.zol.com.cn/include/js/public.js",
		//
		"http://dcbbs.zol.com.cn/include/js/app.js",
		//
		"http://dcbbs.zol.com.cn/include/css/public1.css",
		//
		"http://dcbbs.zol.com.cn/include/css/list1.css",
		//
		"http://dcbbs.zol.com.cn/include/css/list1.css",
		//
		"",
		//
		"main.css", "123"
		//
		];
		var loader = new CSSManager();
		loader.load(csslist, function() {
			output("all loaded");
			//
			var box = document.getElementById("box");
			var w = box.offsetWidth;
			var h = box.offsetHeight;
			output(w + "x" + h);
		});
		//
		setTimeout(function() {
			//csslist.length = 13;
			loader.remove(csslist, function() {
				output("all removed");
				//console.log(document.styleSheets);
			});
		}, 3000);
	};
	//
})(window);